﻿using HiSport.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace HiSport.Core.Util
{
    public static class FunctionUtil
    {
        //创建自旋锁
        private static SpinLock spin = new SpinLock();


        /// <summary>
        /// 获取订单编号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static string GetOrderNum(OrderNumType input)
        {
            bool lockTaken = false;
            var result = "";
            try
            {

                //申请获取锁
                spin.Enter(ref lockTaken);
                result = input + DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            }
            finally
            {
                //工作完毕，或者发生异常时，检测一下当前线程是否占有锁，如果咱有了锁释放它
                //以避免出现死锁的情况
                if (lockTaken)
                    spin.Exit();
            }
            return result;
        }

    }
}
